import { usersStore } from '@/stores/users'
import { usersSessionStore } from '@/stores/usersSession'
import { db } from '../db/db'

const isLogin = () => {
    let isLogin = false

    // 加载本地数据
    const localSession = usersSessionStore()
    if (localSession && localSession.users.username !== '') {
        isLogin = true
    } else {
        const localUsers = usersStore()
        if (localUsers && localUsers.users.username !== '') {

            if (localUsers.users.rememberMe) {
                localSession.setUsers({
                    username: localUsers.users.username,
                    password: localUsers.users.password,
                    enabled: localUsers.users.enabled,
                    realName: localUsers.users.realName,
                    avatar: localUsers.users.avatar,
                    joinDate: localUsers.users.joinDate,
                    usersTypeId: localUsers.users.usersTypeId,
                    rememberMe: localUsers.users.rememberMe
                })
                isLogin = true
            }
        }
    }
    return isLogin
}

const getUsers = () => {
    const localSession = usersSessionStore()
    return localSession.users
}

const getStudent = async (username: string) => {
    return await db.student.where('username').equals(username).toArray()
}

const getOrganize = async (username: string) => {
    return await db.organize.where('username').equals(username).toArray()
}

const setRealName = (realName: string) => {
    const localUsers = usersStore()
    const localSession = usersSessionStore()
    if (localUsers && localUsers.users.username !== '') {
        localUsers.setRealName(realName)
    }

    if (localSession && localSession.users.username !== '') {
        localSession.setRealName(realName)
    }
}

const setPassword = (password: string) => {
    const localUsers = usersStore()
    const localSession = usersSessionStore()
    if (localUsers && localUsers.users.username !== '') {
        localUsers.setPassword(password)
    }

    if (localSession && localSession.users.username !== '') {
        localSession.setPassword(password)
    }
}

const setAvatar = (avatar: string) => {
    const localUsers = usersStore()
    const localSession = usersSessionStore()
    if (localUsers && localUsers.users.username !== '') {
        localUsers.setAvatar(avatar)
    }

    if (localSession && localSession.users.username !== '') {
        localSession.setAvatar(avatar)
    }
}

const logout = () => {
    const localSession = usersSessionStore()
    localSession.setUsers({
        username: '',
        password: '',
        enabled: true,
        realName: '',
        avatar: '',
        joinDate: '',
        usersTypeId: 0,
        rememberMe: false
    })

    const localUsers = usersStore()
    localUsers.setUsers({
        username: '',
        password: '',
        enabled: true,
        realName: '',
        avatar: '',
        joinDate: '',
        usersTypeId: 0,
        rememberMe: false
    })
}
export { isLogin, logout, getUsers, getStudent, getOrganize, setRealName, setPassword, setAvatar }